package com.kidback.bbloc259.ui.view;


import android.graphics.BitmapFactory;
import android.graphics.Color;

import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.Polyline;
import com.amap.api.maps.model.PolylineOptions;
import com.kidback.bbloc259.R;
import com.kidback.bbloc259.base.AbsObserver;
import com.kidback.bbloc259.base.lang.Delay;
import com.kidback.bbloc259.bean.LocationBean;
import com.kidback.bbloc259.network.utils.FreeUtil;
import com.kidback.bbloc259.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;

public class UserViewMapLayer {

    private ViewActivity viewActivity;
    private MapView mapView;

    private List<LatLng> latLngList = new ArrayList<>();
    private Polyline polyline;
    private Marker marker;

    private Disposable disposable;

    public UserViewMapLayer(ViewActivity viewActivity, MapView mapView) {
        this.viewActivity = viewActivity;
        this.mapView = mapView;
    }

    public void onDestroy() {
        FreeUtil.dispose(disposable);
    }

    public void drawRoute(List<LocationBean> list) {

        latLngList.clear();
        for (int i = 0; i < CollectionUtils.size(list); i++) {
            LocationBean locationBean = CollectionUtils.get(list, i);
            latLngList.add(new LatLng(locationBean.latitude, locationBean.longitude));
        }

        if (CollectionUtils.size(latLngList) >= 2) {
            if (polyline != null) {
                polyline.remove();
            }
            polyline = mapView.getMap().addPolyline(new PolylineOptions().
                    addAll(latLngList).width(10).color(Color.argb(255, 255, 1, 1)));
        }


        CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(new CameraPosition(CollectionUtils.last(latLngList), 15, 0, 0));
        mapView.getMap().animateCamera(cameraUpdate);


        Delay.interval(500, new AbsObserver<Long>(){
            @Override
            public void onSubscribe(Disposable d) {
                super.onSubscribe(d);
                disposable = d;
            }

            @Override
            public void onNext(Long aLong) {
                super.onNext(aLong);

                int index = aLong.intValue();
                LatLng latLng = CollectionUtils.get(latLngList, index);


                float zoom = mapView.getMap().getCameraPosition().zoom;
                CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(new CameraPosition(latLng, zoom, 0, 0));
                mapView.getMap().animateCamera(cameraUpdate);

                //绘制最新位置
                MarkerOptions markerOption = new MarkerOptions();
                markerOption.position(latLng);
                markerOption.title("我的位置").snippet("我的位置");
                markerOption.draggable(false);
                markerOption.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
                        .decodeResource(viewActivity.getResources(), R.mipmap.avatar_default)));
                markerOption.setFlat(true);
                if (marker != null) {
                    marker.remove();
                }
                marker = mapView.getMap().addMarker(markerOption);
            }

            @Override
            public void onComplete() {
                super.onComplete();

            }
        });


    }

}
